#pragma once
#include <mythread.hxx>
#include <safeque.hxx>
#include <vector>
#include <map>

#include "auto.hxx"

class FileLog;
class Robot;
class Action;
class TaskManager;
class RobotManager : public Thread
{
public:
	RobotManager(TaskManager* taskManager, DMFun f, int threadpoolsize, int threadpooltopsize, FileLog* filelog = NULL)
		: taskManager(taskManager),
		robots(),
		robotsInUse(),
		DisplayMsg(f),
		threadpoolsize(threadpoolsize),
		threadpooltopsize(threadpooltopsize),
		isShutdownRequested(false),
		_filelog(filelog)
	{
		Start();
	}

	void Shutdown(void);
	virtual void Execute(Thread::Arg arg);

private:
	friend class Robot;
	Action* GetReadyAction(void);
	
private:
	TaskManager* taskManager;
	std::vector<Robot*> robots;
	mutable Mutex robotsInUse;
	const int threadpoolsize;
	const int threadpooltopsize;
	DMFun DisplayMsg;
	bool isShutdownRequested;
	FileLog* _filelog;

	int SpawnRobots(int num = 1, bool isStanding = true);
	void CleanFinishedRobots(void);
};